package com.database.utils;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA by ChouFy on 2020-02-28.
 *
 * @author zhoufy
 */
public class TransactionManager {


    private ConnectionUtils connectionUtils;


    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    public void beginTransaction(DruidDataSource dataSource) throws SQLException {
        connectionUtils.getCurrentThreadConn(dataSource).setAutoCommit(false);
    }

    // 提交事务
    public void commit(DruidDataSource dataSource) throws SQLException {
        connectionUtils.getCurrentThreadConn(dataSource).commit();
    }


    // 回滚事务
    public void rollback(DruidDataSource dataSource) throws SQLException {
        connectionUtils.getCurrentThreadConn(dataSource).rollback();
    }

}
